﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using GreatB2C.Utility;
using GreatB2C.Business;
using GreatB2C.Config.Shop;

namespace GreatB2C.Web.Manage.SystemSet.Shop
{
    public partial class ProducerModify : Inherit.ManagePage
    {
        /// <summary>
        /// 厂商图片上传路径
        /// </summary>
        public const string uploadpath = "/Files/Shop/Producer/";

        Business.Producer BLL = new Producer( );

        protected void Page_Load( object sender , EventArgs e )
        {
            if ( !IsPostBack )
            {
                txtCreateDate.Text = DateTime.Today.ToString( "yyyy-MM-dd" );

                Business.Dictionary dictBLL = new Dictionary( );
                Model.Dictionary dictModel = dictBLL.ListByFieldName( DataDefine.Dictionary.ProducerType.ToString( ) );

                if ( dictModel != null )
                {
                    if ( dictModel.FieldValue.Trim( ) != "" )
                    {
                        // 格式 ： 选项名|是否启用值|默认值
                        int i=0;
                        string[] parts = dictModel.FieldValue.ToString( ).Split( '$' );
                        foreach ( string part in parts )
                        {
                            if ( part.IndexOf( "|" ) > 0 )
                            {
                                string[] choises  = part.Split( '|' );
                                if ( choises.Length == 3 )
                                {
                                    RadlProducerType.Items.Add( new ListItem( choises[ 0 ] , i.ToString( ) ) );
                                    i++;
                                }
                            }
                        }
                    }
                }

                if ( RadlProducerType.Items.Count > 0 )
                    RadlProducerType.Items[ 0 ].Selected = true;

                if ( Request.QueryString[ "ProducerId" ] != null && Request.QueryString[ "ProducerId" ].ToString( ) != "" )
                {
                    LoadData( );
                }
                else
                {
                    litScript.Text = "<script type='text/javascript'>ShowErrorAndRedirect('错误','参数传入错误或者非法进入页面！','ProducerManage.aspx');</script>";
                    return;
                }
            }
        }

        protected void LoadData( )
        {
            if ( Request.QueryString[ "ProducerId" ] != null && Request.QueryString[ "ProducerId" ].ToString( ) != "" )
            {
                Model.Producer model = new GreatB2C.Model.Producer( );
                model.ProducerID = Convert.ToInt32( Request.QueryString[ "ProducerId" ].ToString( ) );
                model = BLL.ListByID( model );
                if ( model != null )
                {
                    RadlProducerType.SelectedValue = model.ProducerType.ToString( );
                    txtProducerName.Text = model.ProducerName;
                    txtProducerShortName.Text = model.ProducerShortName;
                    hidImagSrc.Value = model.ProducerPhoto;
                    imgProducerPhoto.ImageUrl = model.ProducerPhoto;
                    txtCreateDate.Text = model.BirthDay.ToString( "yyyy-MM-dd" );
                    txtAddress.Text = model.Address;
                    txtPostcode.Text = model.Postcode;
                    txtPhone.Text = model.Phone;
                    txtFax.Text = model.Fax;
                    txtEmail.Text = model.Email;
                    txtHomepage.Text = model.Homepage;
                    txtProducerIntro.Text = model.ProducerIntro;
                }
            }
        }

        protected void BtnSubmit_Click( object sender , EventArgs e )
        {
            Model.Producer model = new GreatB2C.Model.Producer( );
            model.ProducerID = Convert.ToInt32( Request.QueryString[ "ProducerId" ].ToString( ) );
            model.ProducerType = Convert.ToInt32( RadlProducerType.SelectedValue.ToString( ) );
            model.ProducerName = txtProducerName.Text.Trim( );
            model.ProducerShortName = txtProducerShortName.Text.Trim( );
            model.ProducerPhoto = hidImagSrc.Value.Trim( );
            model.BirthDay = Convert.ToDateTime( txtCreateDate.Text.Trim( ) );
            model.Address = txtAddress.Text.Trim( );
            model.Postcode = txtPostcode.Text.Trim( );
            model.Phone = txtPhone.Text.Trim( );
            model.Fax = txtFax.Text.Trim( );
            model.Email = txtEmail.Text.Trim( );
            model.Homepage = txtHomepage.Text.Trim( );
            model.ProducerIntro = txtProducerIntro.Text.Trim( );
            model.LastUseTime = DateTime.Now;

            bool isExist = BLL.IsExists( model );
            if ( isExist )
            {
                litScript.Text = "<script type='text/javascript'>ShowError('错误','该厂商信息已经存在，请重新输入！');</script>";
                return;
            }
            else
            {
                bool isSuccess = BLL.Update( model );
                if ( isSuccess )
                {
                    litScript.Text = "<script type='text/javascript'>ShowInfoAndRedirect('操作成功','厂商信息修改成功！','ProducerManage.aspx');</script>";
                    return;
                }
                else
                {
                    litScript.Text = "<script type='text/javascript'>ShowError('操作失败','厂商信息修改失败！');</script>";
                    return;
                }
            }
        }
    }
}
